<?php

/**
 *  合同mo'ban
 * Created by PhpStorm.
 * User: Apache
 * Date: 2019/10/27
 * Time: 10:02
 */
namespace app\api\controller\basemanage;
use app\api\controller\common\ApiBase;
use app\api\controller\common\CommonPlus;
use app\api\controller\common\AuthorCon;
use think\Controller; // den
use think\Db;
class Article extends ApiBase
{

    /**
     *
     * @return \think\response\Json|void
     *  comppub
     *
     */

    public function getList(){
        $page = $this->request->param('pageIndex');
        $keyword = $this->request->param('title');
        $page = max(1,intval($page));
        $pageSize =$this->request->param('pageSize')==""?20:$this->request->param('pageSize');
        $param=" and s.is_delete=0";
        if (!empty($keyword)) {
            $param.=" and s.title like '%$keyword%' ";
        }
        $sql2="select s.* from crm_article as s ";
        $sql2.=" where 1=1 $param";
        $sql2.=" order by s.sort desc";
        $total1=Db::query($sql2);
        $sql2.=" limit ".($page-1)*$pageSize.",$pageSize";
        $list=Db::query($sql2);
        $total=count($total1); //st
        return json(['code'=>1,'msg'=>'success','data'=>['list'=>$list,'total'=>$total]]);
    }


    /**
     * 添加
     * @return \think\response\Json|void
     *  zs_question       zs_question_option
     *  zs_question_type
     */
    public function add(){
        $data['create_time']=time();
        $data['update_time']=time();
        $data['title']=$this->request->param('title');   // 1单选 2多选3  填空
        $data['sort']=$this->request->param('sort');   // 1单选 2多选3  填空
        $data['status']=$this->request->param('status');
        $data['cover']=$this->request->param('cover');
        $data['description']=$this->request->param('description');
        $data['content']=$this->request->param('content');
        $data['recommend_type']=$this->request->param('recommend_type');
        $data['video_path']=$this->request->param('video_path');
        $data['type']=$this->request->param('type');
        $data['path']=$this->request->param('path');
        $res=db('article')->insert($data);
        if(!$res){
            return json(['code'=>0,'msg'=>'操作失败','data'=>'']);
        }
        return json(['code'=>1,'msg'=>'操作成功','data'=>'']);
    }

    /**
     * 删除
     */
    public function del(){
        $id = $this->request->param('ids');
        $ids = explode(',',$id);
        if(empty($id)){
            return json(['code'=>0,'msg'=>'参数有误','data'=>'']);
        }
        $msg='';
        $flag=true;
        foreach ($ids as $id){
            $r = db('article')->where(['id'=>$id])->find();
            if(!$r){
                $flag=false;
                $msg='id:'.$id.'记录不存在';
                break;
            }
        }

        if(!$flag){
            return json(['code'=>0,'msg'=>$msg]);
        }

        foreach ($ids as $id){
            $r = db('article')->where(['id'=>$id])->update(['is_delete'=>1]);
            if(!$r){
                $flag=false;
                $msg='id:'.$id.'删除失败请重试';
                break;
            }
        }

        if(!$flag){
            return json(['code'=>0,'msg'=>$msg]);
        }
        return json(['code'=>1,'msg'=>'操作成功']);

    }


    /**
     * 基本信息修改
     * @return \think\response\Json|void
     */
    public function edit()
    {
//        die();
        $id = $this->request->param('id');
        if (empty($id)) {
            return json(['code' => 0, 'msg' => '参数有误，请核查']);
        }
        $data['update_time']=time();
        $data['title']=$this->request->param('title');   // 1单选 2多选3  填空
        $data['sort']=$this->request->param('sort');   // 1单选 2多选3  填空
        $data['status']=$this->request->param('status');
        $data['description']=$this->request->param('description');
        $data['content']=$this->request->param('content');
        $data['recommend_type']=$this->request->param('recommend_type');
        $data['video_path']=$this->request->param('video_path');
        $data['cover']=$this->request->param('cover');
        $data['type']=$this->request->param('type');
        $data['path']=$this->request->param('path');
        $where['id'] = $id;
        $r=db('article')->where($where)->update($data);
        if (!$r) {
            return json(['code' => 0, 'msg' => '操作失败，请重试']);
        }
        return json(['code' => 1, 'msg' => '操作成功']);
    }

    /**
     * 基本信息修改
     * @return \think\response\Json|void
     */
    public function getDetail()
    {
        $id = $this->request->param('id');
        if (empty($id)) {
            return json(['code' => 0, 'msg' => '参数有误，请核查']);
        }
        $where['id'] = $id;
        $r=db('article')->where($where)->find();
        return json(['code' => 1, 'msg' => '操作成功','data'=>$r]);
    }


    /**
     * 根据公海id获得获取
     * @return \think\response\Json|void
     *   https://gwsp.linyuinfo.com/api/basemanage/Article/wxSendTempinfo
     */

    public function wxSendTempinfo111(){



        var_dump(111);die;
//        $result=db('exhibition')->where(['id'=>$exhibition_id])->find();
//        if($result){
//            $tabName=$result['create_tablename'];
//        }else{
//            return json(['code'=>0,'msg'=>'缺少关键字段','data'=>'']);
//        }
//        return $tabName;
    }


    /**
     * 根据公海id获得获取
     * @return \think\response\Json|void
     *   https://gwsp.linyuinfo.com/api/basemanage/Article/wxSendTempinfo
     */
    public function wxSendTempinfo(){

        global $_W, $_GPC;
//        $openid=$_GPC['openid'];
//        $touser=$params['touser'];
//         var_dump(1111);die;
        $type="orderok";
        $params=array('kc_name'=>'sdf','teachername'=>1,'kc_price'=>23,'uname'=>33);
        if($type=="orderok"){
            $tempid="eA80w39lLSGPxjcu1UCvPXh_3Dq8qz2WwHLAToKaJgY";
//            $items=array('touser'=>$students['dad'],'uname'=>$students['s_name'],'kc_name'=>$kc_name,'kc_price'=>'('.$orderinfo['paytype_name'].')'.$orderinfo['cose'],
//                'teachername'=>$njzr['tname'],'schoolid'=>$order['schoolid'],'kcid'=>$tcourse['id']);
            $url="";// <a href='".$url."' >ces</a>
            // $url=$_W['siteroot'] . 'app/' .$this->createMobileUrl('kcinfo', array( 'schoolid' =>$params['schoolid'],'id'=>$params['kcid'] ));
            $first="恭喜你有成员下订单";
            $keyword1=$params['kc_name'];
            $keyword2=$params['teachername'];
            $keyword3=$params['kc_price'];
            $keyword4=date('Y-m-d');
            $keyword5=$params['uname'];
             $remark="感谢你，用心服务每一位家长！\n\n不开心和有压力的时候，\n深夜睡不着的时候，\n......，\n如何调频到喜悦和平？\n我们在听什么，\n";
            //$remark.="就会构建起心里的影像，\n进而构建外在的实相。\n今晚准时收听，{$params['kc_name']},\n就是选择了一种生活方式。";

        }elseif ($type=="kcup"){
            //课程更新通知
            // xLixhpYbow3wxvItuwpvbkoC12bzIkFe80b2bcmT4lM    uDPJAJ38EM5XVuAT6jLDc_6ixJi_2h-hNFojQFm4vgM
            $tempid="sSFFVloHB5OYwpJ5aY8WYZGvfVMgdtcM0yE2IyYIm2U";
            $first="课程上新通知";
            $url=$params['url'];
            $keyword1=$params['kc_name'];
            $keyword2=$params['kc_type']; // 课程类别
            $keyword3=$params['teachername'];
            $keyword4=date('Y-m-d');
            $remark="爱与教育深耕班，您教子路上最深情的陪伴~~";

            //var_dump(112);die;
        }elseif ($type=="cxjg"){
            // 查询结果通知
//            {{first.DATA}}
//            已上课时：{{keyword1.DATA}}
            //查询时间：{{keyword2.DATA}}
            //统计时间：{{keyword3.DATA}}
            //{{remark.DATA}}
            //
            // var_dump($params);die;
            $tempid="z8QvgA938lSlepSEmFbD19SpDcL3AHdbZnqD8vbI5QI";
            $first=$params['s_name']."你好,你的个人办公昨日数据如下";
            $url=$params['url'];
            $keyword1="";
            $keyword2=date('Y-m-d H:i:s'); // 查询时间
            $keyword3=date('Y-m-d H:i:s'); // 统计时间
            //$keyword4=date('Y-m-d');
            //var_dump(222);die;
            // $para=array('touser'=>$item1['own'],'s_name'=>$item1['s_name'],'url'=>$url,'down1count'=>$down1count,'down2count'=>$down2count);

            $remark="新增B级家长{$params['down1count']}人 \n新增C级家长{$params['down2count']}人\n新增家长共{$params['downtotal']}人";
            $remark.="\n爱与教育深耕班，您教子路上最深情的陪伴~~";

        }
        else{
            $first="";
            $keyword1="";
            $keyword2="";
            $keyword3="";
            $keyword4="";
            $keyword5="";
            $remark="";
        }

        //
        $template_data = array(
            'first' => array(   'value' => $first,
                'color' => '#ff510'),
            'keyword1' => array(
                'value' => $keyword1,
                'color' => '#000000'
            ),
            'keyword2' => array(
                'value' =>$keyword2,
                'color' => '#000000'
            ),
            'keyword3' => array(
                'value' => $keyword3,
                'color' => '#000000'
            ),
            'keyword4' => array(
                'value' => $keyword4,
                'color' => '#000000'
            ),
            'keyword5' => array(
                'value' => $keyword5,
                'color' => '#000000'
            ),
            'remark' => array(
                'value' => $remark ,
                'color' => '#000000'
            ),
        );
        //发送消息给推荐人  own zj   OKrw0tqzdeEUk4R6xws7SnAG9tLSzYpTC_bglRpbcng, eA80w39lLSGPxjcu1UCvPXh_3Dq8qz2WwHLAToKaJgY
        // ff510 墨绿
//        $account_api = WeAccount::create();
//
//        $res=$account_api->sendTplNotice($touser,$tempid,$template_data,$url);

//        return $res;
        // var_dump($account_api);die;

        $openid=array("o7HeyszY854vIz1ul66RkIODwP_0","o7Heys4PIp2o0MDhyrpdJWkY8ZL4","o7Heys_7VZWoff9MwQgIa4DaY51k");
        $template_id="ielXmf8lgMuyP-VVv_t1ND0C1L19HZ4hZAKxpCoguJs";
        $url="#";
        $title="客户：，已完成检测支付";
        $price="0.01";
//        foreach ($openid as $oid){
//            $this->sendMessage($oid,$template_id,$url,$title,$price);
//        }
        $auth= new AuthorCon();
//        $com = new CommonPlus();

        //
        $openid="ofSRDw7QmwgU_tWCb7inQu48xVgM";
        $openid="oXZiH4pKWcB5vkftlCZaFtsXeuqA";// 小程序 兀

//        $res=$auth->sendBirth($openid);
//        $res=$auth->sendBirthxcx($openid);




    }


}
